<div class="pageContent">
	<div class="unitBox" style="margin-left:1px;"> 
		<div class="pageHeader" style="border:1px #B8D0D6 solid">
			<form onsubmit="return navTabSearch(this, 'dmt_list_module');" action="<?cs var:config.cgipath ?>mt_slog?action=list_module" method="post">
			<div class="searchBar">
				<ul class="searchContent" >
					<li>
						<label>应 用：</label>
						<select class='xrk_combox' name="dm_app_id" id="dm_app_id">
							<option value="0"> - 请选择 - </option>
						</select>
					</li>
					<li>
						<label>模 块：</label>
						<select class='xrk_combox' name="dm_module_id" id="dm_module_id">
							<option value="0"> - 请选择 - </option>
						</select>
					</li>
				</ul>
				<div class="subBar">
					<ul>
						<li>
							<button class="buttonActive" onclick='dmOnSearch();'><i class='icon-search icon-large '></i>查找</button>
						</li>
					</ul>
				</div>
			</div>
			</form>
		</div>

		<div class="pageContent" style="border-left:1px #B8D0D6 solid;border-right:1px #B8D0D6 solid">
			<div class="panelBar">
				<ul class="toolBar">
					<li><a href="#" onclick="return dmAddModule();"><i class='icon-plus'></i>添加</a></li>
					<li class="line">line</li>
					<li><a href="#" onclick="return dmDeleteModule();"><i class='icon-minus'></i>删除</a></li>
					<li class="line">line</li>
					<li><a href="#" onclick="return dmModModule();"><i class='icon-edit'></i>修改</a></li>
					<li class="line">line</li>
				</ul>
			</div>

			<div>
				<table class="list" width="100%" layoutH="65" id="dm_table_id"> 
					<thead>
						<tr>
							<th width="40">序号</th>
							<th width="60">模块 id</th>
							<th width="100">模块名称</th>
							<th width="140">所属应用</th>
							<th width="90">创建人</th>
							<th width="100">最近更新时间</th>
							<th width="140">模块描述</th>
							<th width="60">详情</th>
						</tr>
					</thead>
					<tbody id="dm_module_list_tbody">
					</tbody>
				</table>
			</div> 
		</div>
	</div> <!-- unitBox -->
</div>

<script language="javascript" type="text/javascript">
var dm_src_module_list = '<?cs var:config.module_list ?>';
var dm_module_list = $.parseJSON(dm_src_module_list);
var dm_sel_module = "";
var dm_sel_app_idx = ""; 
var dm_sel_detail_app_idx = ""; 

function dmSetSelModule(appIdx, moduleIdx)
{
	dm_sel_app_idx = appIdx;
	dm_sel_module = dm_module_list.applist[appIdx].modulelist[moduleIdx];
}

function dmOnDetail(appIdx, idx)
{
	dm_sel_detail_app_idx = appIdx;
	var moduleinfo = dm_module_list.applist[appIdx].modulelist[idx];
	var op = $.parseJSON('{"mask":true,"maxable":false,"height":340,"width":580}');
	$.pdialog.open("<?cs var:config.docpath?>dmt_dlg_detail_module.html", "dm_dlg_detail",
		"模块："+moduleinfo.name+" 详情", op);
}

function dmSetModulelist()
{
	$("#dm_module_list_tbody").html('');
	if(dm_module_list == null || dm_module_list.app_count <= 0)
		return "";
	var app_id = $('#dm_app_id').val();
	
	var ulist = dm_module_list.applist;
	var uhtml = "";
	for(var i=0, j=1; i < ulist.length; i++)
	{
		if(app_id != 0 && app_id != ulist[i].app_id || ulist[i].module_count <= 0)
			continue;

		var mlist = ulist[i].modulelist;
		var module_id = $('#dm_module_id').val();
		for(var k=0; k < mlist.length; k++, j++) {
			if(module_id != 0 && module_id != mlist[k].id)
				continue;
			uhtml += "<tr onclick='dmSetSelModule("+i+','+k+")'>";
			uhtml += "<td>" + j + "</td>";
			uhtml += "<td>" + mlist[k].id + "</td>";
			uhtml += "<td>" + mlist[k].name + "</td>";
			uhtml += "<td>" + ulist[i].app_name + "</td>";

			uhtml += "<td>" + mlist[k].user_add + "</td>";
			uhtml += "<td>" + mlist[k].mod_time + "</td>";
			uhtml += "<td>" + mlist[k].desc + "</td>";

			var detail = "<a class='detail' href='#' onclick='return dmOnDetail("+i+','+k+");'>详情</a>";
			uhtml += "<td>"+detail+"</td>";
			uhtml += "</tr>";
		}
	}
	$("#dm_module_list_tbody").html(uhtml);
}

function dmAddModule()
{
	if(dm_module_list == null || dm_module_list.app_count <= 0){
		alertMsg.warn("您的应用数目为 0，请先添加应用！");
		return false;
	}
	var url = "<?cs var:config.cgipath?>mt_slog?action=add_module";
	var op = $.parseJSON('{"mask":true,"maxable":false,"height":320,"width":520}'); 
	$.pdialog.open(url, "dm_dlg_add_module", "添加模块", op); 
}

function dmDeleteModule()
{
	if(dm_sel_module == "")
	{
		alertMsg.info("请先点击模块列表选择一个要删除的模块");
		return;
	}

	var msg="确认删除模块：" + dm_sel_module.name + " (id:" + dm_sel_module.id + ") 吗？";
	alertMsg.confirm(msg, {
		okCall: function(){
			var url = "<?cs var:config.cgipath?>mt_slog?action=delete_module"+"&id="+dm_sel_module.id;
			url += '&app_id='+dm_module_list.applist[dm_sel_app_idx].app_id;
			$.get(url, function(dmta){
				if(dmtFirstDealAjaxResponse(dmta))
					return;

				if(dmta.statusCode==200){
					alertMsg.info("删除成功。");
					return navTab.reload();
				}
				else if(data.msg)
					alertMsg.error(data.msg);
				else
					alertMsg.error("删除失败！");
			}, "json"); 
		}
	});
	return;
}

function dmAppChange()
{
	var app_id = $('#dm_app_id').val();
	if(app_id == 0 || app_id != window.localStorage.getItem('dm_app_sel')) {
		window.localStorage.removeItem('dm_module_sel');
	}

	var app_list = dm_module_list.applist;
	var modules = $('#dm_module_id');
	var module_sel = window.localStorage.getItem('dm_module_sel');
	if(module_sel == null)
		modules.html('<option value="0" selected> - 全部 - </option>');
	else
		modules.html('<option value="0"> - 全部 - </option>');
	for(var i=0; i < app_list.length; ++i) 
	{
		if(app_list[i].app_id == app_id) {
			var app = app_list[i];
			for(var j=0; j < app.module_count; ++j)
			{
				var op = $('<option></option>');
				op.val(app.modulelist[j].id).text(app.modulelist[j].name);
				if(module_sel != 0 && app.modulelist[j].id == module_sel)
					op.attr("selected", true);
				modules.append(op);
			}
			break;
		}
	}
}

function dmOnSearch()
{
	var app_id = $('#dm_app_id').val();
	if(app_id == 0)
		window.localStorage.removeItem('dm_app_sel');
	else if(window.localStorage.getItem('dm_app_sel') != app_id)
		window.localStorage.setItem('dm_app_sel', app_id);

	var module_id = $('#dm_module_id').val();
	if(module_id == 0)
		window.localStorage.removeItem('dm_module_sel');
	else if(window.localStorage.getItem('dm_module_sel') != module_id)
		window.localStorage.setItem('dm_module_sel', module_id);

	return true;
}

function dmModModule()
{
	if(dm_sel_module == "")
	{
		alertMsg.info("请先点击模块列表选择一个要修改的模块");
		return;
	}

	var url = "<?cs var:config.cgipath?>mt_slog?action=mod_module";
	url += "&id=" + dm_sel_module.id; 
	url += "&app_id=" + dm_module_list.applist[dm_sel_app_idx].app_id;
	url += "&app_name=" + encodeURIComponent(dm_module_list.applist[dm_sel_app_idx].app_name); 
	url += "&module_name=" + encodeURIComponent(dm_sel_module.name);
	url += "&module_desc=" + encodeURIComponent(dm_sel_module.desc);

	var op = $.parseJSON('{"mask":true,"maxable":false,"height":320,"width":520}'); 
	$.pdialog.open(url, "dm_dlg_mod_module", "修改模块信息", op); 
}

$(document).ready(function(){
	if(dm_module_list.app_count <= 0) {
		alertMsg.info("应用数目为0，请先添加应用！");
		return;
	}
	else {
		var app_sel = window.localStorage.getItem('dm_app_sel');
		var app = $('#dm_app_id');
		app.change(dmAppChange); 
		if(app_sel == null)
			app.html('<option value="0" selected> - 全部 - </option>');
		else
			app.html('');
		var app_list = dm_module_list.applist;
		for(var i=0; i < app_list.length; ++i) {
			var op = $('<option></option>');
			op.val(app_list[i].app_id).text(app_list[i].app_name);
			if(app_sel == app_list[i].app_id)
				op.attr("selected", true);
			app.append(op);
		}
		dmAppChange();
		dmSetModulelist();
	}
});

</script>


